package com.example.thread;

import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.Function;

public class CompletableFuture03 {

    /**
     * https://www.jianshu.com/p/d81cf0beb858
     */
    public void test() {
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello");

        future = future.thenApply(new Function<String, String>() {
            @Override
            public String apply(String s) {

                return s + " World";
            }
        }).thenApply(new Function<String, String>() {
            @Override
            public String apply(String s) {
                return s.toUpperCase();
            }
        });

        try {
            System.out.println(future.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }

    public void testLamda(){
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello")
                .thenApply(s -> s + " World").thenApply(String::toUpperCase);

        try {
            System.out.println(future.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
        CompletableFuture03 c=new CompletableFuture03();
        c.test();


    }

    public void test1() throws InterruptedException {
        // 第一个任务:商品id
        CompletableFuture<String> cfQuery = CompletableFuture.supplyAsync(() -> {
            return query("iphone12");
        });


//        Thread.sleep(5000);

        // 老式写法
/*        CompletableFuture<Object> cfFetch = cfQuery.thenApplyAsync(new Function<String, Object>() {
            @Override
            public Object apply(String s) {
                return comment(s);
            }
        });*/
        // query成功后继续执行下一个任务:
        CompletableFuture<String> cfFetch = cfQuery.thenApplyAsync((code) -> {
            return comment(code);
        });


        //comment成功后打印结果:
        cfFetch.thenAccept((result) -> {
            System.out.println(result);
        });
        // 主线程不要立刻结束，否则CompletableFuture默认使用的线程池会立刻关闭:
        Thread.sleep(2000);

    }


    /****
     * 商品查询
     * @param name
     * @return
     */
    static String query(String name) {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
        }
        int random = new Random().nextInt(10);
        String str = random <= 5 ? "20210709" : "0000";
        System.out.println(str);
        return str;
    }

    /***
     * 秒杀商品的价格、活动判断
     * @param code
     * @return
     */
    static String comment(String code) {
        System.out.println("code:" + code);
        if (code.equals("0000")) {
            return "没有任何评论";
        }
        return "苹果手机没有华为好";

    }
}
